package com.sky.controller.admin;

import com.sky.context.BaseContext;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "管理端订单接口")
@Slf4j
@RequestMapping("/admin/order")
public class OrderAdminController {

    @Autowired
    private OrderService orderService;


    /**
     * 订单搜索
     *
     * @param ordersPageQueryDTO
     * @return
     */
    @ApiOperation("订单搜索")
    @GetMapping("/conditionSearch")
    public Result<PageResult> ConditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("订单搜索{}", ordersPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(orderService.CountOrders(ordersPageQueryDTO));
        pageResult.setRecords(orderService.getOrders(ordersPageQueryDTO));
        return Result.success(pageResult);
    }


    /**
     * 统计各个状态订单数量
     *
     * @return
     */
    @ApiOperation("统计各个状态订单数量")
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> CountOrder() {
        log.info("统计各个状态订单数量");
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();
        orderStatisticsVO = orderService.CountOrderOver();
        return Result.success(orderStatisticsVO);
    }


    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/details/{id}")
    public Result<OrderVO> searchOrder(@PathVariable Integer id) {
        log.info("查询订单详情{}", id);
        OrderVO orderVO = new OrderVO();
        orderVO = orderService.getOrderDetailById(Long.valueOf(id));
        return Result.success(orderVO);
    }


    /**
     * 接单
     *
     * @param id
     * @return
     */
    @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("接单{}", ordersConfirmDTO.getId());
        orderService.confirm(Long.valueOf(ordersConfirmDTO.getId()));
        return Result.success();
    }


    /**
     * 取消订单
     *
     * @param ordersCancelDTO
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result OrdersCancel(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("取消订单{}", ordersCancelDTO);
        orderService.OrderCancel(ordersCancelDTO);
        return Result.success();
    }


    /**
     * 拒单
     *
     * @param ordersRejectionDTO
     * @return
     */
    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("拒单{}", ordersRejectionDTO);
        orderService.OrdersRejectionI(ordersRejectionDTO);
        return Result.success();
    }


    /**
     * 订单派送
     *
     * @param id
     */
    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable Integer id) {
        log.info("派送订单{}", id);
        orderService.delivery(id);
        return Result.success();
    }


    /**
     * 完成订单
     * @param id
     * @return
     */
    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Integer id) {
        log.info("订单完成");
        orderService.complete(id);
        return Result.success();
    }
}
